<?php

namespace Largezhou\Admin;

use Illuminate\Support\ServiceProvider;
use Largezhou\Admin\Form\Form;

class AdminServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Form::registerBuiltinFields();

        $this->loadMenus();

        $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');

        require_once __DIR__.'/helpers.php';

        $this->registerCommands();

        \Admin::loadConfigs();
    }

    protected function loadMenus()
    {
        if (!$this->app->runningInConsole()) {
            \Admin::loadMenus();
        }
    }

    protected function registerCommands()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                Commands\InstallMenus::class,
                Commands\MakeController::class,
            ]);
        }
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->registerAlias();
    }

    protected function registerAlias()
    {
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
        $loader->alias('Admin', Facades\Admin::class);
    }
}
